jQuery(function ($) {
    //获取路径;
     var keyUrl = window.location.pathname.split('/')[2];
    //热门问题
    hot_question();
    function hot_question() {
        var pageOnoff = true;
        var onoff = false;
        generateList(0);
        function generateList(page) {
            var param = {
                "AskCat": {
                    "url": keyUrl
                },
                "Ask[]": {
                    "query": "2",
                    "count": 5,
                    "page": page,
                    "Ask": {
                        "cat_id@": "AskCat/id",
                        "@order": "clicks-",
                        "AskAnswer[]": {
                            "AskAnswer": {
                                "ask_id@": "Ask[]/Ask/id",
                                "@column": "id"
                            }
                        }
                    }
                },
                "total@": "/Ask[]/total"
            }
            send_ajax('post', '/get', param, function callback(data) {
                if (data.code == 200 && data['Ask[]']){
                    //评价标签渲染
                    var tag = function tag(option, id) {
                        var param = {
                            "AskKeywords[]": {
                                "AskKeywords": {
                                    "ask_id": id,
                                    "KeywordsTags": {
                                        "id@": "/keywords_id",
                                        "type": "2"
                                    }
                                }
                            }
                        };

                        send_ajax('post', '/get', param, function callback(res) {
                            if (res.code == 200 && res["AskKeywords[]"]) {
                                option.AskKeywords = res["AskKeywords[]"];
                                var asks_id = option.AskKeywords;
                                for (var n = 0; n < tag_el.length; n++) {
                                    for (var j = 0; j < asks_id.length; j++) {
                                        if ($(tag_el[n]).attr('data-ask-id') == asks_id[j].ask_id&&asks_id[j].KeywordsTags) {
                                            $(tag_el[n]).append('<a href="/question/question_ask_tag.html?tag_id=' + asks_id[j].id + '" target="_blank">' + (asks_id[j].KeywordsTags ? asks_id[j].KeywordsTags.title : "") + '</a>');
                                        }
                                    }
                                };
                            }
                        });
                    };
                    if(pageOnoff){
                        twbsPagination(data.total);
                        pageOnoff=false;
                    }
                    var data = data['Ask[]'];
                    var html = "";
                    //分页按钮
                    for (var i = 0; i < data.length; i++) {
                        tag(data[i], data[i].id);
                        html += '<li class="clearfix">\n                            <div class="msg">\n                                <a href="/article-' + data[i].id + '.html" class="asks_title" target="_blank">' + data[i].title + '</a>\n                                <span class="ask_tag_tip" data-ask-id=' + data[i].id + '>\n                                </span>\n                            </div>\n                            <span class="reply_num">\n                                <em>' + (data[i]["AskAnswer[]"] ? data[i]["AskAnswer[]"].length : 0) + '</em>\n                                \u56DE\u7B54\n                            </span>\n                            <span class="ask_time">' + data[i].update_time.substring(0, 11) + '</span>\n                        </li>';
                    }
                    $('.hot_question').html(html);
                    var tag_el = $('.hot_question .ask_tag_tip');;
                } else {
                    $('.hot_question').html('<li>暂时没有有问题</li>');
                }
            });
        }
        function twbsPagination(res) {
            Page({
                num:Math.ceil(res / 5),					//页码数
                startnum:1,				//指定页码
                elem:$('#pagination_b'),		//指定的元素
                callback:function(page){	//回调函数
                    if (page != 1) {
                        onoff = true;
                    }
                    if (onoff) {
                        generateList(page - 1);
                    }
                }
            });
        }
    };
    //待解决的问题
    unresolved_question();
    function unresolved_question() {
        var pageOnoff = true;
        var onoff = false;
        generateList(0);
        function generateList(page) {
            var param = {
                "AskCat": {
                    "url": keyUrl
                },
                "Ask[]": {
                    "query": "2",
                    "count": 5,
                    "page": page,
                    "Ask": {
                        "cat_id@": "AskCat/id",
                        "@order": "rank-",
                        "closed_flag":0,
                        "AskAnswer[]": {
                            "AskAnswer": {
                                "ask_id@": "Ask[]/Ask/id",
                                "@column": "id"
                            }
                        }
                    }
                },
                "total@": "/Ask[]/total"
            }
            send_ajax('post', '/get', param, function callback(data) {
                if (data.code == 200 && data['Ask[]']) {
                    //评价标签渲染
                    var tag = function tag(option, id) {
                        var param = {
                            "AskKeywords[]": {
                                "AskKeywords": {
                                    "ask_id": id,
                                    "KeywordsTags": {
                                        "id@": "/keywords_id",
                                        "type": "2"
                                    }
                                }
                            }
                        };
                        send_ajax('post', '/get', param, function callback(data) {
                            if (data.code == 200 && data["AskKeywords[]"]) {
                                option.AskKeywords = data["AskKeywords[]"];
                                var asks_id = option.AskKeywords;
                                for (var n = 0; n < tag_el.length; n++) {
                                    for (var j = 0; j < asks_id.length; j++) {
                                        if ($(tag_el[n]).attr('data-ask-id') == asks_id[j].ask_id&&asks_id[j].KeywordsTags) {
                                            $(tag_el[n]).append('<a href="/question/question_ask_tag.html?tag_id=' + asks_id[j].id + '" target="_blank">' + (asks_id[j].KeywordsTags ? asks_id[j].KeywordsTags.title : "") + '</a>');
                                        }
                                    }
                                };
                            }
                        });
                    };
                    if(pageOnoff){
                        twbsPaginations(data.total);
                        pageOnoff=false;
                    }
                    var data = data['Ask[]'];
                    var html = "";
                    //分页按钮
                    for (var i = 0; i < data.length; i++) {
                        tag(data[i], data[i].id);
                        html += '<li class="clearfix">\n                            <div class="msg">\n                                <a href="/article-' + data[i].id + '.html" class="asks_title" target="_blank">' + data[i].title + '</a>\n                                <span class="ask_tag_tip" data-ask-id=' + data[i].id + '>\n                                   \n                                </span>\n                            </div>\n                            <span class="reply_num">\n                                <em>' + (data[i]["AskAnswer[]"] ? data[i]["AskAnswer[]"].length : 0) + '</em>\n                                \u56DE\u7B54\n                            </span>\n                            <span class="ask_time">' + data[i].update_time.substring(0, 11) + '</span>\n                        </li>';
                    }
                    $('.unresolved_question').html(html);
                    var tag_el = $('.unresolved_question .ask_tag_tip');;
                } else {
                    $('.unresolved_question').html('<li>暂时没有有问题</li>');
                }
            });
        };
        function twbsPaginations(res) {
            Page({
                num:Math.ceil(res / 5),					//页码数
                startnum:1,				//指定页码
                elem:$('#pagination_c'),		//指定的元素
                callback:function(page){	//回调函数
                    if (page != 1) {
                        onoff = true;
                    }
                    if (onoff) {
                        generateList(page - 1);
                    }
                }
            });
        };
    }
    //已解决的问题
    resolved_question();
    function resolved_question() {
        var pageOnoff = true;
        var onoff = false;
        generateList(0);
        function generateList(page) {
            var param = {
                "AskCat": {
                    "url": keyUrl
                },
                "Ask[]": {
                    "query": "2",
                    "count": 5,
                    "page": page,
                    "Ask": {
                        "cat_id@": "AskCat/id",
                        "@order": "rank-",
                        "closed_flag":1,
                        "AskAnswer[]": {
                            "AskAnswer": {
                                "ask_id@": "Ask[]/Ask/id",
                                "@column": "id"
                            }
                        }
                    }
                },
                "total@": "/Ask[]/total"
            }
            send_ajax('post', '/get', param, function callback(data) {
                if (data.code == 200 && data['Ask[]']) {
                    //评价标签渲染
                    var tag = function tag(option, id) {
                        var param = {
                            "AskKeywords[]": {
                                "AskKeywords": {
                                    "ask_id": id,
                                    "KeywordsTags": {
                                        "id@": "/keywords_id",
                                        "type": "2"
                                    }
                                }
                            }
                        };
                        send_ajax('post', '/get', param, function callback(data) {
                            if (data.code == 200 && data["AskKeywords[]"]) {
                                option.AskKeywords = data["AskKeywords[]"];
                                var asks_id = option.AskKeywords;
                                for (var n = 0; n < tag_el.length; n++) {
                                    for (var j = 0; j < asks_id.length; j++) {
                                        if ($(tag_el[n]).attr('data-ask-id') == asks_id[j].ask_id&&asks_id[j].KeywordsTags) {
                                            $(tag_el[n]).append('<a href="/question/question_ask_tag.html?tag_id=' + asks_id[j].id + '" target="_blank">' + (asks_id[j].KeywordsTags ? asks_id[j].KeywordsTags.title : "") + '</a>');
                                        }
                                    }
                                };
                            }
                        });
                    };
                    if(pageOnoff){
                        twbsPaginations(data.total);
                        pageOnoff=false;
                    }
                    var data = data['Ask[]'];
                    var html = "";
                    //分页按钮
                    for (var i = 0; i < data.length; i++) {
                        tag(data[i], data[i].id);
                        html += '<li class="clearfix">\n                            <div class="msg">\n                                <a href="/article-' + data[i].id + '.html" class="asks_title" target="_blank">' + data[i].title + '</a>\n                                <span class="ask_tag_tip" data-ask-id=' + data[i].id + '>\n                                    \n                                </span>\n                            </div>\n                            <span class="reply_num">\n                                <em>' + (data[i]["AskAnswer[]"] ? data[i]["AskAnswer[]"].length : 0) + '</em>\n                                \u56DE\u7B54\n                            </span>\n                            <span class="ask_time">' + data[i].update_time.substring(0, 11) + '</span>\n                        </li>';
                    };
                    $('.resolved_question').html(html);
                    var tag_el = $('.resolved_question .ask_tag_tip');;
                } else {
                    $('.resolved_question').html('<li>暂时没有有问题</li>');
                }
            });
        };
        function twbsPaginations(res) {
           
            Page({
                num:Math.ceil(res / 5),					//页码数
                startnum:1,				//指定页码
                elem:$('#pagination_d'),		//指定的元素
                callback:function(page){	//回调函数
                    if (page != 1) {
                        onoff = true;
                    };
                    if (onoff) {
                        generateList(page - 1);
                    };
                }
            });
        };
    };
});